<template>
  <div class="control">
    <transition name="move">
      <div class="reduce" v-show="showCount" @click.stop.prevent="reduce">
        <span class="inner icon-remove_circle_outline"></span>
      </div>
    </transition>
    <div class="count" v-show="showCount">
      {{food.count}}
    </div>
    <div class="add icon-add_circle" @click.stop.prevent="add($event)">
    </div>
  </div>
</template>

<script>
export default {
  name: 'CartControl',
  props: {
    food: {
      type: Object
    }
  },
  computed: {
    showCount () {
      return this.food.count > 0
    }
  },
  methods: {
    add (e) {
      if (!this.food.count) {
        this.$set(this.food, 'count', 1)
      } else {
        this.food.count++
      }
      let evt = window.event || e
      this.$emit('add', evt.target)
    },
    reduce () {
      if (this.food.count > 0) {
        this.food.count--
      }
    }
  }
}
</script>

<style scoped lang="stylus">
.control
  .reduce
    display inline-block
    padding .12rem
    .inner
      font-size .48rem
      line-height .48rem
      color rgb(0, 160, 220)
    &.move-enter,&.move-leave-to
      opacity 0
      transform rotate(180deg)
    &.move-enter-active,&.move-leave-active
      transition all .4s linear
      .inner
        transform rotate(0)
  .count
    display inline-block
    vertical-align top
    width .24rem
    padding-top .12rem
    line-height .48rem
    text-align center
    font-size .2rem
    color rgb(147, 153, 159)
  .add
    display inline-block
    font-size .48rem
    line-height .48rem
    color rgb(0, 160, 220)
    padding .12rem
</style>
